// This is a part of the WceUi library.
// Copyright (C) WceUi Team
// All rights reserved.
//
// Coded by Hui Xu
//
#include "StdAfx.h"
#include "WceUiDbClick.h"
#include "WceUiLayer.h"


CWceUiDbClick::CWceUiDbClick(void)
{
	m_pLayer = NULL;
	m_dwDelay = UINT_MAX;
	m_xDist = INT_MAX;
	m_yDist = INT_MAX;

	m_MouseStatus = E_LBUTTON_BEGIN;
	m_dwLastDownTickCount = 0;

	m_ptDownLast.x = -INT_MAX;
	m_ptDownLast.y = -INT_MAX;
}

CWceUiDbClick::~CWceUiDbClick(void)
{
	UnInit();
}


void CWceUiDbClick::Init(CWceUiLayer* pLayer, DWORD dwDelay /* = DBCLICK_DEFALUT_DELAY */, 
			int xDist /* = DBCLICK_DEFALUT_X_DIST */, int yDist /* = DBCLICK_DEFALUT_Y_DIST */)
{
	ASSERT(NULL != pLayer);
	
	m_pLayer = pLayer;
	m_dwDelay = dwDelay;
	m_xDist = xDist;
	m_yDist = yDist;

	m_MouseStatus = E_LBUTTON_BEGIN;
	m_dwLastDownTickCount = 0;

	m_ptDownLast.x = -INT_MAX;
	m_ptDownLast.y = -INT_MAX;
}

void CWceUiDbClick::UnInit()
{
	m_pLayer = NULL;
}

BOOL CWceUiDbClick::IsDbClick(UINT nFlags, POINT point)
{
	ASSERT(NULL != m_pLayer);
	BOOL bDbClick = FALSE;

	DWORD dwCurTickCount = GetTickCount();

	if (E_LBUTTON_DOWN_UP == m_MouseStatus)
	{
		if (dwCurTickCount - m_dwLastDownTickCount < m_dwDelay 	// double click
			&& abs(point.x - m_ptDownLast.x) < m_xDist
			&& abs(point.y - m_ptDownLast.y) < m_yDist)
		{
			//send the double click message
			m_pLayer->OnLButtonDblClk(nFlags, point);
			bDbClick = TRUE;
			m_MouseStatus = E_LBUTTON_BEGIN; //reset the state
		}
	}

	//if it is not a double click message, it must be a left button down message.
	//set state to E_LBUTTON_DOWN
	if (!bDbClick)
	{
		m_ptDownLast = point;
		m_dwLastDownTickCount = dwCurTickCount;
		m_MouseStatus = E_LBUTTON_DOWN;
	}

	return bDbClick;
}


void CWceUiDbClick::RecordLButtonUp(UINT nFlags, POINT point)
{
	UNREFERENCED_PARAMETER(nFlags);
	UNREFERENCED_PARAMETER(point);

	ASSERT(NULL != m_pLayer);
	if (E_LBUTTON_DOWN == m_MouseStatus)
	{
		m_MouseStatus = E_LBUTTON_DOWN_UP;
	}
}